<?php

namespace frontend\controllers;

use Yii;
use bc\controller\FrontController;
use yii\web\Session;
use bc\util\Util;
use bc\model\OrderModel;
use frontend\models\order\BindModel;
use bc\ar\MActiveRecord;
use bc\model\wx\report\CodeModel;
use bc\util\Sms;
use bc\controller\WxOAuthController;
use bc\model\order\DetailModel;
use bc\model\ProductModel;
use bc\model\AddressModel;

class OrderController extends FrontController
{
	protected $order_url = 'http://wx.zhishimama.com/shop/mobile/admin/order_api.php?act=order_query&page_size=100&user_name=';
	protected $shop_base_url = 'http://wx.zhishimama.com/shop/';
	
	
	/**
	 * 
	 */
	public function actionSearch()
	{
		$this->setOAuth ();
		
		$session = new Session ();
		$user = $session->get ( 'user' );
		if (! $user)
			$this->redirect ( '/' );
		
		$mobile = $this->get ( 'mobile', '' );
		$name = $this->get ( 'name', '' );
		
		$list ['result'] = false;
		
		if ($mobile != '' && $name != '')
		{
			// 写入cookie 7776000
			setcookie ( 'search_name', $name, time () + 86400 * 15 ); // 半个月
			setcookie ( 'search_mobile', $mobile, time () + 86400 * 15 ); // 半个月
		}
		else
		{
			$name = $_COOKIE ['search_name'] ? $_COOKIE ['search_name'] : '';
			$mobile = $_COOKIE ['search_mobile'] ? $_COOKIE ['search_mobile'] : '';
		}
		
		$model = new \bc\model\wx\ReportModel ();
		
		if ($mobile != '' && $name != '')
		{
			$params = '`openid` = ""';
			$params .= ' AND `name` = "' . $name . '"';
			$params .= ' AND `mobile` = "' . $mobile . '"';
			
			$model->sort = 'report_id DESC';
			$list ['list'] = $model->getList ( $params );
			
			$list ['result'] = true;
		}
		
		$view ['mobile'] = $mobile;
		$view ['name'] = $name;
		$view ['list'] = $list;
		return $this->render ( 'search', $view );
	}
	
	
	/**
	 * 
	 */
	public function actionOfflinereport()
	{
		$id = $this->get ( 'id' );
		
		$session = new Session ();
		$user = $session->get ( 'user' );
		
		if (! $id || ! $user)
			$this->redirect ( '/' );
		
		$model = new \bc\model\wx\ReportModel ();
		$params ['report_id'] = $id;
		$row = $model->getRow ( $params );
		
		if ($row)
		{
			if (file_exists ( UPLOAD_PATH . $row ['file'] ))
			{
				// 记录查询记录
				$logModel = new \bc\model\offline\ReportLogModel ();
				$logData ['openid'] = $user ['openid'];
				$logData ['log_report_id'] = $row ['report_id'];
				$logData ['create_time'] = time ();
				$logModel->add ( $logData );
				
				$this->redirect ( STATIC_IMAGE . $row ['file'] );
			}
		}
		else
			$this->redirect ( '/' );
	}
	
	/**
	 * 校验验证码
	 */
	public function actionValidcode()
	{
		$msg ['error'] = 1;
		
		if ($this->getRequest ()->isAjax == true)
		{
			
			$user_name = $this->post ( 'user_name' );
			$mobile = $this->post ( 'mobile' );
			$code = $this->post ( 'code' );
			$model = new \bc\model\wx\ReportModel ();
			$parmas ['name'] = $user_name;
			$params ['mobile'] = $mobile;
			
			$codeModel = new CodeModel ();
			$codeModel->sort = 'id DESC';
			$code_row = $codeModel->getList ( [ 
					'mobile' => $mobile,
					'code' => $code 
			] );
			$msg ['data'] = json_encode ( $code_row );
			if (! $code_row [0])
			{
				$msg ['msg'] = '请输入正确的验证码';
			}
			elseif ($code_row [0] ['create_time'] < time () - 60)
			{
				$msg ['msg'] = '验证码已过期';
			}
			else
			{
				$row = $model->getRow ( $params );
				if (! $row)
				{
					$msg ['msg'] = '亲爱哒，您的无创正在快马加鞭检测中，再耐心等候下哈，感谢您的理解与支持～';
				}
				else
				{
					$msg ['error'] = 0;
				}
			}
		}
		
		else
		{
			$msg ['msg'] = '请求异常';
		}
		
		echo json_encode ( $msg );
		exit ();
	}
	
	/**
	 * ajax方式发送验证码
	 */
	public function actionSend()
	{
		$msg = [ ];
		
		if ($this->getRequest ()->isAjax == true)
		{
			$user_name = $this->post ( 'user_name' );
			$mobile = $this->post ( 'mobile' );
			$model = new \bc\model\wx\ReportModel ();
			$parmas ['name'] = $user_name;
			$params ['mobile'] = $mobile;
			
			// 13651886355
			
			// 创建一个新的验证码
			
			$arr = [ ];
			
			for($i = 0; $i < 4; $i ++)
			{
				$rand = rand ( 0, 9 );
				$arr [] = $rand;
			}
			
			$code = implode ( '', $arr );
			$data ['code'] = $code;
			$data ['mobile'] = $mobile;
			$data ['create_time'] = time ();
			
			$codeModel = new CodeModel ();
			$codeModel->sort = 'id DESC';
			$code_row = $codeModel->getList ( [ 
					'mobile' => $mobile 
			] );
			$codeModel->add ( $data );
			
			// 发送短信验证码
			$sms_msg = '验证码：' . $code . '，本次验证码60s内有效，用于您正在进行的无创报告查询';
			
			$sms = new Sms ();
			$sms->setMobile ( $mobile );
			$sms->setText ( $sms_msg );
			$result = $sms->send ();
			$msg ['result'] = $result;
			$msg ['msg'] = '验证码已发送!';
		}
		else
		{
			$msg ['msg'] = '请求参数错误！';
		}
		
		echo json_encode ( $msg );
		exit ();
	}

}

